using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Comm;

public partial class Web_Admin_Grouping_Grouping_Add : AdminPageBase
{
    private int id = 0;
    private BLL.Grouping bll = new BLL.Grouping();
    protected void Page_Load(object sender, EventArgs e)
    {
        id = base.QueryInt("id");
        But_AsSave.Visible = base.CheckPopedomInfo(1023, 2);
        if (!IsPostBack)
        {
            loadType();
            loadData();
        }
    }

    /// <summary>
    /// 加载类型
    /// </summary>
    private void loadType()
    {
        drpLang.DataSource = Comm.PublicType.getLanguage();
        drpLang.DataBind();
        chk_Classification.DataSource = new BLL.Classification().GetList(0, "", "Ordinal asc");
        chk_Classification.DataBind();
    }

    /// <summary>
    /// 加载数据
    /// </summary>
    private void loadData()
    {
        if (id > 0)
        {
            Model.Grouping model = bll.GetModel(id);
            if (model == null)
            {
                MessageBox.Show("该对象不存在");
            }

            But_Save.Visible = base.CheckPopedomInfo(1023, 4);
            this.Title = navigation1.Title = lit_title.Text = "分组编辑";
            txt_UserID.Text = model.UserID.ToString();
            if (!string.IsNullOrEmpty(model.Classification))
            {
                foreach (ListItem item in chk_Classification.Items)
                {
                    item.Selected = model.Classification.Contains("," + item.Value + ",");
                }
            }
            txt_Content.Text = model.Content;
            txt_UpNum.Text = model.UpNum.ToString();
            txt_DownNum.Text = model.DownNum.ToString();
            txt_AddTime.Text = model.AddTime.Value.ToString("yyyy-MM-dd HH:mm:ss");
            drpLang.SelectedValue = model.Lang;
        }
        else
        {
            But_Save.Visible = base.CheckPopedomInfo(1023, 2);
        }
    }

    /// <summary>
    /// 保存
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void But_Save_Click(object sender, EventArgs e)
    {
        save();
    }

    /// <summary>
    /// 另存为
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void But_AsSave_Click(object sender, EventArgs e)
    {
        id = 0;
        save();
    }

    /// <summary>
    /// 保存
    /// </summary>
    private void save()
    {
        Model.Grouping model = myValidate();
        if (model == null)
        {
            return;
        }

        if (id > 0)
        {
            if (bll.Update(model,"ID="+id) < 1)
            {
                MessageBox.Show("修改失败");
            }
            base.AddLog(2, "分组管理ID:" + id);
        }
        else
        {
            int i = bll.Add(model);
            if (i < 1)
            {
                MessageBox.Show("添加失败");
            }
            base.AddLog(1, "分组管理ID:" + i);
        }

        Response.Redirect("Grouping_List.aspx");
    }

    /// <summary>
    /// 验证
    /// </summary>
    /// <returns></returns>
    private Model.Grouping myValidate()
    {
        string UserID = txt_UserID.Text;
        string Classification = "";
        foreach (ListItem item in chk_Classification.Items)
        {
            Classification += item.Selected ? item.Value + "," : "";
        }
        if (!string.IsNullOrEmpty(Classification))
        {
            Classification = "," + Classification;
        }
        string Content = txt_Content.Text;
        string UpNum = txt_UpNum.Text;
        string DownNum = txt_DownNum.Text;
        string AddTime = txt_AddTime.Text;
        string Lang = drpLang.SelectedValue;

        Model.Grouping model = new Model.Grouping();
        model.UserID = int.Parse(UserID);
        model.Classification = Classification;
        model.Content = Content;
        model.UpNum = int.Parse(UpNum);
        model.DownNum = int.Parse(DownNum);
        model.AddTime = DateTime.Parse(AddTime);
        model.Lang = Lang;
        
        return model;
    }
}
